package com.java.second_goods.controller;

import com.java.second_goods.common.AjaxResult;
import com.java.second_goods.entity.Promotion;
import com.java.second_goods.service.PromotionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 活动信息
 */
@RestController
@RequestMapping("/api/promotion")
@Tag(name = "活动管理", description = "活动相关接口")
public class PromotionController extends BaseController {

    @Autowired
    private PromotionService promotionService;

    /**
     * 获取活动列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取活动列表", description = "获取活动列表")
    public AjaxResult list(Promotion promotion) {
        List<Promotion> list = promotionService.selectPromotionList(promotion);
        return success(list);
    }

    /**
     * 获取活动详细信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取活动详细信息", description = "根据活动ID获取活动详细信息")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(promotionService.selectPromotionById(id));
    }

    /**
     * 新增活动
     */
    @PostMapping
    @Operation(summary = "新增活动", description = "新增活动")
    public AjaxResult add(@RequestBody Promotion promotion) {
        return toAjax(promotionService.insertPromotion(promotion));
    }

    /**
     * 修改活动
     */
    @PutMapping
    @Operation(summary = "修改活动", description = "修改活动")
    public AjaxResult edit(@RequestBody Promotion promotion) {
        return toAjax(promotionService.updatePromotion(promotion));
    }

    /**
     * 删除活动
     */
    @DeleteMapping("/{ids}")
    @Operation(summary = "删除活动", description = "删除活动")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(promotionService.deletePromotionByIds(ids));
    }

    /**
     * 获取有效的活动列表
     */
    @GetMapping("/enabled")
    @Operation(summary = "获取有效的活动列表", description = "获取有效的活动列表")
    public AjaxResult getEnabledPromotions() {
        List<Promotion> promotions = promotionService.selectEnabledPromotions();
        return success(promotions);
    }

    /**
     * 根据类型查询活动列表
     */
    @GetMapping("/type/{type}")
    @Operation(summary = "根据类型查询活动列表", description = "根据类型查询活动列表")
    public AjaxResult getPromotionsByType(@PathVariable("type") String type) {
        List<Promotion> promotions = promotionService.selectPromotionsByType(type);
        return success(promotions);
    }

    /**
     * 更新活动状态
     */
    @PutMapping("/status/{promotionId}/{status}")
    @Operation(summary = "更新活动状态", description = "更新活动状态")
    public AjaxResult updateStatus(@PathVariable("promotionId") Integer promotionId,
                                   @PathVariable("status") Integer status) {
        return toAjax(promotionService.updatePromotionStatus(promotionId, status));
    }
}